package opengl.platform.ohos;

import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public class GLUtils {
    public static void texImage2D(int target, int level, int mBitmapFormat, PixelMap mBitmap, int border) {
        texImage2DImpl(target, level, mBitmapFormat, mBitmap, border);
    }

    public static void texImage2DImpl(int target, int level, int mBitmapFormat, PixelMap mBitmap, int border) {
        Log.d("GLUtils", "texImage2D paras:" + target + " paras:" + level + " paras:" + mBitmapFormat + " paras:" + border);
/*    if(mBitmapFormat != GLES20.GL_RGBA)
      throw new RuntimeException("texImage2D only support RGBA");
    long pixels = mBitmap.getPixelBytesCapacity();
    int pixelsInt = (int)(pixels >> 2);
    IntBuffer buffer = IntBuffer.allocate(pixelsInt);
    mBitmap.readPixels(buffer);
    int[] bufferInt = buffer.array();
    byte[] bufferRGBA = new byte[pixelsInt * 4];
    for(int i = 0; i < pixelsInt; i++){
      bufferRGBA[i*4+0] = (byte) (bufferInt[i]>>16 & 0xFF); //R
      bufferRGBA[i*4+1] = (byte) (bufferInt[i]>>8 & 0xFF); //G
      bufferRGBA[i*4+2] = (byte) (bufferInt[i] & 0xFF); //B
      bufferRGBA[i*4+3] = (byte) (bufferInt[i]>>24 & 0xFF); //A
    }
    buffer.clear();
    Size bitmapsize = mBitmap.getImageInfo().size;
    ByteBuffer bufferImage = ByteBuffer.wrap(bufferRGBA);
    bufferImage.position(0);
    ohos.agp.render.opengl.GLES20.glTexImage2D(target, level, mBitmapFormat, bitmapsize.width,
      bitmapsize.height, border, mBitmapFormat, GLES20.GL_UNSIGNED_BYTE, bufferImage);*/
        ohos.opengl.GLUtils.texImage2D(target, level, mBitmapFormat, mBitmap, GLES20.GL_UNSIGNED_BYTE, 0);
    }

    public static void texSubImage2D(int target, int level, int xoffset, int yoffset,
                                     PixelMap mBitmap, int format, int type) {
        Log.d("GLUtils", "texSubImage2D paras:" + target + " paras:" + level + " paras:" + xoffset + " paras:" + yoffset + " paras:" + format + " paras:" + type);
        if (format != GLES20.GL_RGBA)
            throw new RuntimeException("texImage2D only support RGBA");
        long pixels = mBitmap.getPixelBytesCapacity();
        int pixelsInt = (int) (pixels >> 2);
        IntBuffer buffer = IntBuffer.allocate(pixelsInt);
        mBitmap.readPixels(buffer);
        int[] bufferInt = buffer.array();
        byte[] bufferRGBA = new byte[pixelsInt * 4];
        for (int i = 0; i < pixelsInt; i++) {
            bufferRGBA[i * 4 + 0] = (byte) (bufferInt[i] >> 16 & 0xFF); //R
            bufferRGBA[i * 4 + 1] = (byte) (bufferInt[i] >> 8 & 0xFF); //G
            bufferRGBA[i * 4 + 2] = (byte) (bufferInt[i] & 0xFF); //B
            bufferRGBA[i * 4 + 3] = (byte) (bufferInt[i] >> 24 & 0xFF); //A
        }
        buffer.clear();
        Size bitmapsize = mBitmap.getImageInfo().size;
        ByteBuffer bufferImage = ByteBuffer.wrap(bufferRGBA);
        bufferImage.position(0);
        ohos.agp.render.opengl.GLES20.glTexSubImage2D(target, level, xoffset, yoffset, bitmapsize.width,
                bitmapsize.height, format, GLES20.GL_UNSIGNED_BYTE, bufferImage);
    }

    public static void texImage2D(int target, int level, PixelMap bitmap,
                                  int border) {
        Log.d("GLUtils", "texImage2D paras:" + target + " paras:" + level + " paras:" + border);
        texImage2DImpl(target, level, GLES20.GL_RGBA, bitmap, border);
    }
}
